package com.lw.mall.service2.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lw.mall.entity.DescImg;
import com.lw.mall.mapper.DescImgMapper;
import com.lw.mall.service2.DescImgService;
import com.lw.mall.utils.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lw
 * @since 2022-10-03
 */
@Service
public class DescImgServiceImpl extends ServiceImpl<DescImgMapper, DescImg> implements DescImgService {

    @Override
    public void saveDescImg(MultipartFile[] imageDesc, Integer id) throws Exception {
        int i=0;
        final int length = imageDesc.length;
        //修改为StringBuffer保证线程安全
        final StringBuffer stringBuilder = new StringBuffer();
        for (MultipartFile file : imageDesc) {
            final String fileAliyun = FileUtil.saveFileAliyun(file);
            i++;
            if(i<length){
                stringBuilder.append(fileAliyun+",");
            }else {
                stringBuilder.append(fileAliyun);
            }
        }
        final DescImg descImg = new DescImg();
        descImg.setProductId(id);
        descImg.setImgUrl(stringBuilder.toString());
        baseMapper.insert(descImg);
    }
}
